<?php
/*
 * 康复训练信息编辑及展示接口
 * */
class Apiv1_RecoveredtrainController extends Ctrl_Apiatk{
    //编辑    
    public function editAction(){
        $p = $_REQUEST;
        $pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pType = empty($p['type'])?Tool_Fnc::ajaxMsg('训练类型不能为空'):Tool_Fnc::safe_string($p['type']);
        $pDuration = empty($p['duration'])?Tool_Fnc::ajaxMsg('训练时长不能为空'):Tool_Fnc::safe_string($p['duration']);
        $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间点不能为空'):Tool_Fnc::safe_string($p['at']);

        $tSMO = new SeniorModel;
        $tId = $tSMO->field('id')->where('id = '.$pId)->fRow()['id'];
        if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}

        $tRTMO = new RecoveredtrainModel;
        $tTime = time();
        $tRid = $tRTMO->field('id,created')->where('sid = '.$pId.' and date = "'.date('Y-m-d',$tTime).'" and at = '.$pAt)->fRow(); 
        if(empty($tRid)){//记录为空，添加操作
            $tData = array(
                'type' => $pType,
                'duration' => $pDuration,
                'created' => $tTime,
                'sid' => $pId,
                'date' => date('Y-m-d',$tTime),
                'week' => $this->getTimeWeek($tTime),
                'at' => $pAt,
            );
            if(!$tRTMO->insert($tData)){
                Tool_Fnc::ajaxMsg('编辑失败 001');
            }
        }else{//记录存在，修改操作
            $tData = array(
                'type' => $pType,
                'duration' => $pDuration,
                'created' => $tTime,
                'date' => date('Y-m-d',$tTime),
                'week' => $this->getTimeWeek($tTime),
                'sid' => $pId,
                'at' => $pAt,
                'id' => $tRid['id'],
        );
            if(!$tRTMO->update($tData)){
                Tool_Fnc::ajaxMsg('编辑失败 002');
            }
        } 
        Tool_Fnc::ajaxMsg('编辑成功',1);
    }
    //展示
    public function listAction(){
        $p = $_REQUEST;
        $pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pDate = empty($p['date'])?'':Tool_Fnc::safe_string($p['date']);
        $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间点不能为空'):Tool_Fnc::safe_string($p['at']);

        $tSMO = new SeniorModel;
        $tId = $tSMO->field('id')->where('id = '.$pId)->fRow()['id'];
        if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}

        $tRTMO = new RecoveredtrainModel;
        //默认查询本周数据
        $time = time();
        $w_day=date("w",$time);
        if($w_day=='1'){
                $cflag = '+0';
                $lflag = '-1';
        }else{
                $cflag = '-1';
                $lflag = '-2';
        }
        $weekstar = strtotime(date('Y-m-d',strtotime("$cflag week Monday", $time))); //本周一零点的时间戳
        $weekend = strtotime(date('Y-m-d',strtotime("$cflag week Monday", $time)))+7*24*3600;//本周末零点的时间戳
        $tWhere = ' sid = '.$pId.' and created > '.$weekstar.' and created < '.$weekend.' and at = '.$pAt;
        if(!empty($pDate)){
           $tWhere = ' sid = '.$pId.' and date = "'.$pDate.'" and at = '.$pAt; 
        }
        $tDatas = $tRTMO->field('id,week,sid,type,duration')->where($tWhere)->order('id desc')->fList();
        if(!empty($tDatas)){
            Tool_Fnc::ajaxMsg('',1,$tDatas);
        }
    }
    //获取周几
    private function getTimeWeek($time, $i = 0) {
        $weekarray = array('1', '2', '3', "4", "5", "6", "7");
        $oneD = 24 * 60 * 60;
        return $weekarray[date("w", $time + $oneD * ($i-1))];
    }
}
?>
